package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.Color;

import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.utils.Tools;

public class DWRadioButton extends DWControl
{
	private int m_num_w = 0;//横向显示数量
	private String[][] m_contents = null;//标签内容
	private int m_select_index = 0;//选择下标
	private int m_color = Color.WHITE;
	private int m_arc_w = 20;//每个圆的宽度
	private int m_arc_h = 20;//每个圆的高度
	private int m_radio_w = 0;//每个标签宽度,包括圆
	private int m_radio_h = 0;//每个标签高度,包括圆
	private Bitmap img_rect = null;
	private Bitmap img_select = null;
	/**
	 * 创建一个单选列表
	 * @param contents 每个选项的标签
	 * @param num_w 横向显示个数
	 * @param w 单个宽度
	 * @param h 单个高度
	 */
	public DWRadioButton(String[] contents, int num_w, int w, int h, Bitmap rect, Bitmap select) {
		if (contents == null) {
			throw new NullPointerException("DWRadioButton contents is null!");
		}
		m_num_w = num_w;
		m_radio_w = w;
		m_radio_h = h;
		m_arc_w = rect.getWidth();
		m_arc_h = rect.getHeight();
		img_rect = rect;
		img_select = select;
		int len = contents.length;
		int col = len / num_w + (len % num_w > 0 ? 1 : 0);
		m_contents = new String[col][];
		for (int i = 0, k = 0, j = 0; i < m_contents.length; i++) {
			k = contents.length - i * num_w;
			m_contents[i] = new String[k > num_w ? num_w : k];
			for (j = 0; j < m_contents[i].length; j++) {
				m_contents[i][j] = contents[i * num_w + j];
			}
		}
		setShowWideHigh(w * num_w , h * col);
	}
	
	public void render(DWGraphics g)
	{
		setClip(g);
		int w = m_arc_w + (m_radio_w - m_arc_w) / 2;
		int h = (int)((m_radio_h - m_arc_h) / 2);
		int fh = (int)((m_radio_h - DWFont.getDefaultFont().getFontHeight()) / 2);
		for (int i = 0, j = 0, fw = 0, x = 0, y = 0; i < m_contents.length; i++) {
			for (j = 0; j < m_contents[i].length; j++) {
				fw = (int)DWFont.getDefaultFont().stringWidth(m_contents[i][j]) / 2;
				x = m_show_x + j * m_radio_w;
				y = m_show_y + i * m_radio_h;
				g.drawBitmap(img_rect, x, y + h, Tools.LEFT_TOP);
				g.drawString(m_contents[i][j], m_color, x + w - fw, y + fh, Tools.LEFT_TOP);
				if (m_select_index == i * m_num_w + j) {
					g.drawBitmap(img_select, x + m_arc_w / 2, y + h + m_arc_h / 2, Tools.HCENTER_VCENTER);
				}
//				if (m_is_show) {
//					g.drawRect(x, y, m_radio_w - 1, m_radio_h - 1);
//				}
			}
		}
	}
	
	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		setSubClip(g, x, y, w, h, offsetx, offsety);
		int tw = m_arc_w + (m_radio_w - m_arc_w) / 2;
		int th = (int)((m_radio_h - m_arc_h) / 2);
		int fh = (int)((m_radio_h - DWFont.getDefaultFont().getFontHeight()) / 2);
		for (int i = 0, j = 0, fw = 0, tx = 0, ty = 0; i < m_contents.length; i++) {
			for (j = 0; j < m_contents[i].length; j++) {
				fw = (int)DWFont.getDefaultFont().stringWidth(m_contents[i][j]) / 2;
				tx = x + m_show_x + j * m_radio_w - offsetx;
				ty = y + m_show_y + i * m_radio_h - offsety;
				g.drawBitmap(img_rect, tx, ty + th, Tools.LEFT_TOP);
				g.drawString(m_contents[i][j], m_color, tx + tw - fw, ty + fh, Tools.LEFT_TOP);
				if (m_select_index == i * m_num_w + j) {
					g.drawBitmap(img_select, tx + m_arc_w / 2, ty + th + m_arc_h / 2, Tools.HCENTER_VCENTER);
				}
//				if (m_is_show) {
//					g.drawRect(tx, ty, m_radio_w - 1, m_radio_h - 1);
//				}
			}
		}
	}

	public void logic()
	{
		
	}
	
	/**
	 * 得到选择下标
	 * @return
	 */
	public int getSelectIndex() {
		return m_select_index;
	}
	
	/**
	 * 设置选择下标
	 * @param index
	 */
	public void setSelectIndex(int index) {
		if (index < 0) {
			return;
		}
		m_select_index = index;
	}
	
	public void refreshControl()
	{
		
	}

	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			float newx = x - m_show_x;
			float newy = y - m_show_y;
			for (int i = 0, j = 0; i < m_contents.length; i++) {
				for (j = 0; j < m_contents[i].length; j++) {
					if (newx > j * m_radio_w && newx < j * m_radio_w + m_radio_w
							&& newy > i * m_radio_h && newy < i * m_radio_h + m_radio_h) {
						m_select_index = i * m_num_w + j;
						if (m_listener != null) {
							m_listener.OnClick();
						}
						return true;
					}
				}
			}
			return true;
		}
		return false;
	}

}
